﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
namespace JTicket.UI.Extention.IHandler
{
    /// <summary>
    /// 判断套票
    /// </summary>
    public class TaoCheckHandler : IHttpHandler
    {
        #region IHttpHandler 成员

        public bool IsReusable
        {
            get { return false; }
        }

        public void ProcessRequest(HttpContext context)
        {
            HttpRequest request = context.Request;
            HttpResponse response = context.Response;
            string TitleCode = request.QueryString["TitleCode"].ToString();
            string Typ = string.Empty;
            string[] Cooke;
            //获取系统设置套票
            List<Model.D_TaoTab> List = DAL.D_TaoTab.Getlist(TitleCode);
            if (List.Count > 0)
            {
                Dictionary<string, int> TikcetNameDic = new Dictionary<string, int>();
                //计算选择的票
                if (request.Cookies["TicketName"] != null)
                {
                    Cooke = request.QueryString["NameList"].ToString().TrimEnd('|').Split('|');
                    if (Cooke.Length > 0)
                    {
                        for (int i = 0; i <= Cooke.Length - 1; i++)
                        {
                            if (TikcetNameDic.ContainsKey(Cooke[i]))
                            {
                                int count = TikcetNameDic[Cooke[i]];
                                TikcetNameDic[Cooke[i]] = count + 1;
                            }
                            else
                                TikcetNameDic.Add(Cooke[i], 1);
                        }
                    }
                }
                //判断
                Dictionary<string, int> TaoDic = new Dictionary<string, int>();
                string[] zh;
                foreach (var model in List)
                {
                    zh = model.TaoPortfolio.TrimEnd('|').Split(new[] { '|', ',' });
                    if (zh.Length > 0)
                    {
                        for (int j = 0; j <= zh.Length - 1; j++)
                        {
                            if (TaoDic.ContainsKey(zh[j]))
                            {
                                int count1 = TaoDic[zh[j]];
                                TaoDic[zh[j]] = count1 + 1;
                            }
                            else
                                TaoDic.Add(zh[j], 1);
                        }
                        if (TikcetNameDic.Count == TaoDic.Count)
                        {
                            // TikcetNameDic.SequenceEqual(TaoDic);
                            int ct = 0, flag = 0, mc = 1;
                            foreach (var x in TikcetNameDic)
                            {
                                if (TaoDic.ContainsKey(x.Key))
                                {
                                    ct = TaoDic[x.Key];
                                    mc = x.Value / ct;
                                    if (x.Value % ct == 0)
                                        flag++;
                                }
                            }
                            if (flag == TaoDic.Count)
                            {
                                //符合
                                response.Write(model.TaoCode + "," + mc);
                                response.End();
                            }
                        }
                    }
                }
                response.Write("false");
                response.End();
            }
            else
            {
                response.Write("false");
                response.End();
            }
        }
        #endregion
    }
}